function autoSetHeight(){
    var winHeight=0;
    if (window.innerHeight)
        winHeight = window.innerHeight;
    else if ((document.body) && (document.body.clientHeight))
        winHeight = document.body.clientHeight;
    if (document.documentElement && document.documentElement.clientHeight)
        winHeight = document.documentElement.clientHeight;
    //DIV高度为浏览器窗口的高度
    document.getElementById("char_content").style.height= (winHeight-203) +"px";
    document.getElementById("nick_list").style.height= (winHeight-70) +"px";
    
}
window.onresize=autoSetHeight;

function keyCtrlAndOrEnter() {
	if (event.ctrlKey && event.keyCode == 13) {
		event.returnValue = false;
		event.cancel = true;
		document.getElementById("input").value = document
				.getElementById("input").value
				+ '\n';
		return;
	}
	if (event.keyCode == 13) {
		event.returnValue = false;
		event.cancel = true;
		document.getElementById("send").click();
		return;
	}
}
function send() {
	if($("#input").val()==""){
		$("#input").val("");
		return;
	}
    $.ajax({
        url: "/OnlineChatRoomMyBaties/receive",
        data: { nickname: $("#nick").text(), message: $("#input").val()},
        type: "post",
        datatype: "html"
       
    });
    $("#input").val("");
}
function online() {
	$.ajax({
		url: "/OnlineChatRoomMyBaties/online",
		data: { nickname: $("#nick").text()},
		type: "post",
		datatype: "html",
		success:function(result){
			if(result=="quit"){
				alert("请注意，已被顶号");
				window.location.href="login.html";
			}
		}	
	});
}
function quit(){
	$.ajax({
        url: "/OnlineChatRoomMyBaties/quit",
        data: { nickname: $("#nick").text()},
        type: "post",
        datatype: "html",
        success:function(){
        	window.location.href="login.html"; 
        }
    });
}
function receive() {
    $.ajax({
        url: "/OnlineChatRoomMyBaties/send",
        data: { nickname: $("#nick").text()},
        type: "post",
        datatype: "json",
        success:function(result){
        	 var nickname=$("#nick").text();
        	 var model = eval("("+result+")");
        	 var msg="";
        	 for(var i = 0;i<model.msgs.length;i++){
        		 if(model.msgs[i].nick==nickname){
        			 msg =msg + "<div class=\"message\"><div class=\"mine_nink\">"+nickname+":</div><div class=\"mine_time\">"+model.msgs[i].timeStr+"</div><div class=\"mine_msg\">"+model.msgs[i].message+"</div></div>";
        		 }
        		 else{
        			 msg =msg + "<div class=\"message\"><div class=\"others_nick\">"+model.msgs[i].nick+":</div><div class=\"others_time\">"+model.msgs[i].timeStr+"</div><div class=\"others_msg\">"+model.msgs[i].message+"</div></div>";
        		 }
             }
        	 var nicklist="";
        	 for(var i=0;i<model.nicks.length;i++){
        		 nicklist=nicklist+"<li>"+model.nicks[i]+"</li>";
        	 }
        	 
        	$("#char_content").append(msg);
        	$("#nick_list_in").empty();
        	$("#nick_list_in").append(nicklist);
        	if(msg!=""){
        		cc = document.getElementById('char_content');
        		cc.scrollTop=Math.max(0,cc.scrollHeight-cc.offsetHeight);
        	}
        }
    });
}